﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ConsoleAppCheckDir
{
    class Program
    {
        static void Main(string[] args)
        {
            //args = new string[] { "/p", @"D:\test\source" };
            if (args.Length < 2)
            {
                Output("argument should be given like /p D:/test.");
                return;
            }

            var path = string.Empty;
            for (int i = 0; i < args.Length; ++i)
            {
                var arg = args[i];
                if (arg.IsEquals("/p", "-p", "--p", "/path", "-path", "--path"))
                {
                    if (i < args.Length - 1)
                    {
                        path = args[i + 1];
                        break;
                    }
                }
            }

            if (path.IsNullOrEmpty() || !Directory.Exists(path))
            {
                Output("the path specified is not exists.");
                return;
            }

            var dirs = Directory.GetDirectories(path);
            var list = new List<string>();
            var listName = new List<string>();
            var notList = new List<string>();
            foreach (var dir in dirs)
            {
                var files = Directory.GetFiles(dir, ".", SearchOption.AllDirectories);
                if (files.Length > 0)
                {
                    list.Add(dir);
                    listName.Add(Path.GetFileName(dir));
                }
                else
                {
                    notList.Add(dir);
                }
            }
            Output("the dirs is empty:");
            Output(string.Join("\n", notList));
            Output("=============================");
            Output("the dirs is not empty:");
            Output(string.Join("\n", list));
            Output("clear the empty dirs?(Y/n)", false);
            var res = Console.ReadLine();
            if (res.IsNullOrEmpty() || res.IsEquals("yes", "y"))
            {
                foreach (var dir in notList)
                {
                    Directory.Delete(dir);
                }
                Output($"clear the {notList.Count} empty dirs.");
            }

            Output("=============================");
            Output("the name of the dir that is not empty:");
            Output(string.Join(",", listName));
            Output("byte!");
        }

        static void Main2(string[] args)
        {
            var source =
                @"100,10029,10038,10043,10062,10065,10127,10131,10152,10153,10165,10179,10182,10185,102,10275,10287,10297,10298,10336,10338,10340,10341,10345,10351,10352,10373,10413,10418,10419,10424,10441,10442,10447,10450,10455,10459,10461,10479,10481,10484,10485,10487,10497,10553,10594,10603,10631,10648,10655,10665,10672,10682,10688,10718,10722,10757,10759,10773,10783,10791,10793,10797,10800,10804,10806,10870,10893,10895,10920,10931,11026,115,1152,1170,1171,1174,1175,1179,1204,1214,123,1247,1249,1264,1267,12722,12801,129,1310,1313,1314,1322,1323,1324,1337,1338,1342,1343,1352,1353,1367,1390,13910,13911,1392,13926,14061,14128,14130,14180,14185,14190,14191,14196,14212,14217,14231,14234,14236,14273,14295,14299,14331,14333,14339,14354,14415,14429,14439,14445,14446,14453,14481,14483,14492,14501,14502,14504,14513,14517,14519,14539,14562,14569,14576,14577,14595,14623,14637,14646,14653,14674,14678,14688,14696,14732,14758,14759,14764,14785,14791,14837,14846,14847,14851,14853,14875,14886,14901,14927,1495,14967,14996,15034,15047,15053,15054,15061,15064,15065,15068,15069,15080,15103,15110,15120,15121,15124,15131,15177,15185,15187,15241,15249,15253,15264,15309,15320,15348,15369,15370,15371,15388,15422,15423,15495,15501,15502,15520,15523,15572,15602,15610,15619,15633,15634,15642,15643,15650,15659,15660,15663,15664,15672,15687,15697,15708,15716,15720,15730,15734,15756,15758,15759,15761,15768,15775,15809,15827,15828,15873,15882,15889,15891,15904,15910,15915,15916,15917,15921,15922,15924,15929,15930,15933,15935,15936,15959,15963,15975,15976,15981,15989,15998,15999,16000,16007,16011,16017,16034,16035,1605,16051,16055,16057,16059,16062,16064,16068,16069,16074,16075,16076,16084,16085,16098,16101,16103,16109,16111,16120,16134,16170,16391,16393,16396,16401,16431,16481,16484,16485,16486,16489,16491,16492,16497,16501,16503,16506,16512,16514,16515,16516,16524,16525,16527,16532,16544,16547,16549,16582,16584,16592,16594,16600,16601,16602,16605,16608,16611,16612,16613,16615,16617,16632,16633,16634,16639,16691,16692,16694,16728,1676,1677,1678,1679,16796,16799,16808,16841,16843,16844,16847,16853,16864,16879,16880,16899,169,16900,16901,16902,16906,16914,16915,16917,16920,16924,16928,16929,16930,16932,16948,16950,16953,16955,16958,16961,16962,16964,16972,16975,16976,16978,16981,16991,16994,17005,17011,17020,17021,17022,17023,17029,17030,17031,17036,17040,17056,17064,17068,17069,17071,17072,17073,17074,17075,17080,17081,17085,17087,17100,17108,17109,17111,17113,17114,17119,17122,17130,17131,17132,17134,17145,17148,17151,17152,17153,17154,17155,17156,17157,17158,17162,17163,17164,17167,17171,17184,17188,17191,17244,17245,17246,17251,1740,17480,17483,17487,17507,17508,17509,17511,17516,17517,17518,17520,17521,17565,17589,17594,17596,17597,17598,17599,17600,17622,17625,17630,1766,17732,17740,17742,17752,17759,17763,17768,1779,17804,17818,1791,1794,1803,1811,1829,1833,1834,1882,1997,214,215,223,233,234,235,236,256,265,281,282,2889,304,336,346,3472,3476,348,3491,3527,39,390,3916,4225,4226,4369,44,460,4814,4843,492,500,504,51,510,511,519,5273,530,532,533,5366,555,5788,595,6081,6090,6267,630,6418,649,6536,6574,6644,6673,6679,6844,6895,693,6978,6980,7167,7258,7264,7269,7275,7278,7332,75,7518,7557,7594,7624,7701,8696,88,9208,9212,9223,9242,9246,9248,9286,9287,9349,9377,9386,9388,940,9436,9452,9460,95,9602,9603,9620,9621,9623,9624,9635,9692,9704,9705,9715,9751,9771,9772,9780,9784,9800,9819,9872,9877,9881,9906,9912,9917,9953,9970,9978,9981,9999";
            var items = source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder sb = new StringBuilder();
            foreach (var item in items)
            {
                sb.AppendFormat("'{0}',", item);
            }

            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - 1, 1);
            }
            File.AppendAllText("temp.txt", sb.ToString(), Encoding.UTF8);
            Output(sb.ToString());
        }

        static void Output(string msg, bool bReturn = true)
        {
            if (bReturn)
                Console.WriteLine(msg);
            else
                Console.Write(msg);
        }
    }
}
